<script>
import {getToken} from "@/store/token";

export default {
  name: "refund",
  props: ["orderId", "returnTotal"],
  data() {
    return {
      state: {
        order: {
          userId: JSON.parse(getToken("user")).id,
          orderId: "",
          returnCause: "",
          returnTotal: 0
        }
      },
      refundVisible: true,
      rules: {
        returnCause: {required: true, message: '请输入退款原因', trigger: 'blur'}
      }
    }
  },
  methods: {
    submitAddForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.state.order.orderId = this.orderId
          this.state.order.returnTotal = this.returnTotal
          this.$api.afterSale.createAfterSales(this.state.order).then(res => {
            if (res.data.success) {
              this.$message.success("申请成功等待管理员确认！")
              this.refundVisible = false
            }
          }).catch(() => {
            this.$message.error("服务器端错误")
          })
        } else {
          return false;
        }
      });

    }, cancelAddSubmit(formName) {
      this.$refs[formName].resetFields();
      this.refundVisible = false
    },
  }, watch: {
    refundVisible() {
      this.$emit('changeVal', this.refundVisible)
    }
  }
}

</script>

<template>
  <el-dialog title="申请退货退款" :visible.sync="refundVisible">
    <el-form :model="state.order" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="退款金额">
        <el-input readonly v-model="returnTotal"></el-input>
      </el-form-item>
      <el-form-item label="退货原因" prop="returnCause">
        <el-input v-model="state.order.returnCause"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitAddForm('ruleForm')">确定</el-button>
        <el-button @click="cancelAddSubmit('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped>

</style>